<?php 
session_start();
if ($_SESSION['auth'] != true && $_SESSION['rule'] != "READER") {
	die('Not log in');
}
include("includes/reader_lib.php");
$FULL = 1;
$MINI = 0;
$SHOWALL = empty($_GET['id']); // id means channel's id
$key = $_GET['key'];
if(!$SHOWALL) {
	$cid = intval($_GET['id']);
} else {
	$cid = (int)!$SHOWALL;
}
if(!empty($_GET['page'])) {
	$page = intval($_GET['page']);
}

$s = getReaderSetting($_SESSION['userdata']['username']); //s = array(bgcolor, newsperpage, fullmode)
$perpage = intval($s['newsperpage']);
$display = intval($s['fullmode']);

if(empty($page) || $page < 0) {
	$page = 1;
}
if(empty($perpage) || $perpage < 0) {
	$perpage = 10;
}

$items = searchContent($_SESSION['userdata']['username'], $key, $cid, $page, $perpage);
$num_items = getNumSearchContent($_SESSION['userdata']['username'], $key, $cid);
$lastpage = ceil($num_items / $perpage);
//$items = arrayToPage($items, $page, $perpage);
$title = $SHOWALL ? 'Show All' : getTitle($cid);
$cSetting = getChannelSetting($_SESSION['userdata']['username'], $cid);
$bgcolor = $SHOWALL? 'FFFFFF' : $cSetting['color'] ;

$num_new_items = getMsgNum($_SESSION['userdata']['username'], $cid);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="itelement.css" rel="stylesheet" type="text/css" />
<script src="script/jquery.js" type="text/javascript"></script>
<script src="script/flooble.js" type="text/javascript"></script>
<script src="script/php.default.js" type="text/javascript"></script>
<script src="script/readerscript_head.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

var timeout = 180000;
var NO_UPDATE = <? echo "'$NO_UPDATE';"; ?>;

$(document).ready (
	function ()
	{
		var cid = <? echo $cid; ?>;
		
		// select cDrop
		$('#cDrop').children('[@value='+cid+']').attr('selected','selected');
		
		// bind clicking item toggle
		$('.title').click
		(
			function(event)
			{
				$(this).parent().parent().next().toggle(500);
			}
		);
		
		//wrap title
		wrapTitle('<? echo $title; ?>', '#cTitle');
		
		//handle window resize
		$(window).resize
		(
			function()
			{
				wrapTitle('<? echo $title; ?>', '#cTitle');
			}
		);

		//bind clicking unsubscribe
		$('#unsub').click
		(
			function(event)
			{
				$(this).text('unsubscribing..');
				$.get("reader_operation.php?action=unsubscribe&id="+cid,
					function(data){
						redirect(
							'0', 
							"<? echo $_SERVER['HTTP_HOST']; ?>", 
							"<? echo $_SERVER['PHP_SELF']; ?>"
						);	//redirect ->SHOWALL
					} );
			}
		);
		//bind clicking channel dropdown
		$('#cDrop').change
		(
			function(event)
			{
				var cid = 'id='+$(this).children("[@selected]").val();
				
				redirect(cid,"<? echo $_SERVER['HTTP_HOST']; ?>", "<? echo $_SERVER['PHP_SELF']; ?>");
			}
		);
		
		$('#refresh_btn').click(
			function(event)
			{
				var cid = 'id='+$('#cDrop').children("[@selected]").val();
				
				redirect(cid,"<? echo $_SERVER['HTTP_HOST']; ?>", "<? echo $_SERVER['PHP_SELF']; ?>");
			}
		);
		
		$('div.item-title').mouseover(function (event)
		{
			$(this).siblings('div.floatLayer').toggle(500);
		});
		$('div.item-title').mouseout(function (event)
		{
			$(this).siblings('div.floatLayer').toggle(500);
		});
		
<?php if(!$SHOWALL) { ?>
		//bind clicking color chooser
		$('#color').click(
			function()
			{
				pickColor('color', 
				function()
				{
					var color = $('#colorfield').val();
					//$(document).attr('bgColor' , '#' + color);
					$('.item').attr('style', 'background-color: #' + color);
					$.get(
						"reader_operation.php?action=changecolor&id=" + cid + "&color=" + color,
						function(data) { }
					);
				});
			});
<?php }  ?>
	}
);
</script>

<style type="text/css">
.main{
	padding-top:30px;
	padding-bottom:30px;
}
/*endif MOZZILLA*/
.main1 {	padding-top:30px;
	padding-bottom:30px;
}

.new-items {
	margin-top: 25px;
	margin-bottom: 25px;
	padding-right: 5px;
	padding-left: 5px;
	padding-top: 3px;
	border-top: 3px double #006699;
	border-right: 5px outset #006699;
	border-bottom: 5px outset #006699;
	border-left: 3px double #006699;
	background: #CCCCCC;
}
#refresh_btn{
	margin-right: 5px;
	border-top: thin dashed #99FF00;
	border-right-color: #99FF00;
	border-bottom-color: #99FF00;
	border-left-color: #99FF00;
	background: #999999;
	margin-left: 5px;
	font-family: Tahoma;
	font-weight: bold;
	color: #FFFF00;
}
</style>
<!--[if lte IE 6]>
<style type="text/css">

#container{overflow:hidden;}
html{overflow:hidden;}
#viewer-header2,#viewer-tail2{
	position: absolute;
}
</style>
<![endif]-->
</head>

<body>

<div id="viewer-header2">
  <table>
    <tr>
      <td class="viewer-head-left"><span id="cTitle" title="<? echo $title; ?>"><?php echo $title; ?></span></td>
      <td class="viewer-head-right"  style="font-size:small;"><?php if(!$SHOWALL){ ?>

        |&nbsp;<a href="#" id="color" style="color:#FFFFFF">color</a>
        <input name="hidden" 
		type="hidden" id="colorfield" title="Color Picker" value="ff0000" size="7" />
        |&nbsp;<a href="#" id="unsub" style="color:#FFFFFF">unsubscribe</a>
        <?php } ?>
        <?php
	$channels = viewChannel($_SESSION['userdata']['username'],$_SESSION['userdata']['rank']);
	if($channels != ''){
		echo "<select id='cDrop' style='width:100px'>";
		echo "<option value='0'>show all</option>";
		foreach($channels as $c){
			echo "<option value='".$c['id']."'>".$c['title']."</option>";
		}
		echo "</select>";
	}
	?>
        <input name="refresh" type="button" id="refresh_btn" value="refresh this channel" />
		<span id="number_msg"><?php echo $num_new_items['unread']; ?> unread message(s) from <?php echo $num_new_items['all']; ?> messages.</span></td>
    </tr>
  </table>
</div>
<div id="viewer-tail2">
  <table>
    <tr style="color:#FFFFFF;">
      <td> search&nbsp;
          <input name="text" type="text" id="searchText" size="30"/>
        &nbsp;
        <input name="button" type="button" onclick="location.href='reader_read_items.php?page=1&amp;perpage=<?php echo $perpage;?>&amp;id=<?php echo $cid;?>&amp;key='+document.getElementById('searchText').value" value="  go  "/>
      </td>
      <td style="font-size:small; vertical-align:bottom">&nbsp;page <? echo $page; ?>
          <?php
			if($page > 1){
				//1
				echo "|&nbsp;<a href=\"?page=1&perpage=$perpage&id=$cid&key=$key\" 
						style=\"color:#ffffff;font-size:small\">first</a>&nbsp;";
				//prev
				echo "|&nbsp;<a href=\"?page=".($page-1)."&perpage=$perpage&id=$cid&key=$key\" 
						style=\"color:#ffffff;font-size:small\">prev</a>&nbsp;";
			}
			if($page < $lastpage) {
				//next
				echo "|&nbsp;<a href=\"?page=".($page+1)."&perpage=$perpage&id=$cid&key=$key\" 
						style=\"color:#ffffff;font-size:small\">next</a>&nbsp;";
				//last
				echo "|&nbsp;<a href=\"?page=$lastpage&perpage=$perpage&id=$cid&key=$key\" 
						style=\"color:#ffffff;font-size:small\">last</a>&nbsp;";
			}
			?>
      </td>
    </tr>
  </table>
</div>
<div class="main1">
  <div id="status">&nbsp;</div>
  <div class="viewer-container" id="container">
    <?php
	if($display == 0) // Do not show the item's content.
	{
		$display = "none";
	}
	else
	{
		$display = "block";
	}
?>
    <!-- BEGIN NEW UPDATE -->
    <div class="new-items">      
	  <div class="item-head">        
		<div class="item-title"> <a class="title">ข่าวมาใหม่</a></div>&nbsp;
		<span id="new-alert">
		<img src="img/window-new.png" title="ข่าวมาใหม่" />
		</span>
        <div class="item-time">&nbsp;</div>
        <div class="item-author">&nbsp;</div>
      </div>
      <div class="item-content" style="display:none">&nbsp;</div>
      <div class="item-tail">&nbsp;</div>
    </div>
    <!-- END NEW UPDATE -->
    <?php
	if($items == '' || count($items) < 0)
	{
		echo '<div class="item-title">ไม่พบข่าว</div>';
	}
	else
	{
		foreach($items as $item)
		{
	?>
    <div class="item" style="background-color: #<?php echo $item['color']; ?>">
      <div class="item-head">
        <div class="item-title"> <a class="title"> <?php echo htmlspecialchars_decode($item['title'], ENT_QUOTES); ?> </a></div>
        <div class="item-time">
          <?php
			echo $item['pubdate'];
			if(strtotime($item['pubdate']) != strtotime($item['real_pubDate'])) {
				echo " (" . $item['real_pubDate'] . ")";
			}
		?>
        </div>
        <div class="item-author"><? echo $item[4]; ?> - by <?php echo $item['author']; ?></div>
      </div>
      <div class="item-content" style="display: <?php echo $display; ?>"> <?php echo htmlspecialchars_decode($item['description'], ENT_QUOTES); ?> </div>
      <div class="item-tail">&nbsp;</div>
    </div>
    <?php
		}
	}
	?>
  </div>
</div>
</body>
<script type="text/javascript" src="script/readerscript_body.js"></script>
<script type="text/javascript">
setParameter
(
	'<?php echo $_SESSION['userdata']['username']; ?>', 
	'<?php echo $key; ?>', 
	'<?php echo $cid; ?>'
);

startTimer();
</script>
</html>
<?php
//setAlreadyRead($cid);
$db->sql_close();
?>